package cn.edu.nju.software.cdiy.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.CPU;
import cn.edu.nju.software.cdiy.bean.Chassis;
import cn.edu.nju.software.cdiy.bean.Disk;
import cn.edu.nju.software.cdiy.bean.Driver;
import cn.edu.nju.software.cdiy.bean.Graphics;
import cn.edu.nju.software.cdiy.bean.HardwareSet;
import cn.edu.nju.software.cdiy.bean.MainBoard;
import cn.edu.nju.software.cdiy.bean.Memory;
import cn.edu.nju.software.cdiy.bean.Monitor;
import cn.edu.nju.software.cdiy.bean.NetworkAdapter;
import cn.edu.nju.software.cdiy.bean.Power;
import cn.edu.nju.software.cdiy.bean.Radiator;
import cn.edu.nju.software.cdiy.bean.Scheme;
import cn.edu.nju.software.cdiy.bean.SoundBox;
import cn.edu.nju.software.cdiy.bean.SoundCard;
import cn.edu.nju.software.cdiy.dao.CPUDao;
import cn.edu.nju.software.cdiy.dao.ChassisDao;
import cn.edu.nju.software.cdiy.dao.DiskDao;
import cn.edu.nju.software.cdiy.dao.DriverDao;
import cn.edu.nju.software.cdiy.dao.GraphicsDao;
import cn.edu.nju.software.cdiy.dao.MainBoardDao;
import cn.edu.nju.software.cdiy.dao.MemoryDao;
import cn.edu.nju.software.cdiy.dao.MonitorDao;
import cn.edu.nju.software.cdiy.dao.NetworkAdapterDao;
import cn.edu.nju.software.cdiy.dao.PowerDao;
import cn.edu.nju.software.cdiy.dao.RadiatorDao;
import cn.edu.nju.software.cdiy.dao.SoundBoxDao;
import cn.edu.nju.software.cdiy.dao.SoundCardDao;
import cn.edu.nju.software.cdiy.dao.ValueDao;
import cn.edu.nju.software.cdiy.service.UtilService;

public class UtilServiceImpl implements UtilService {
	
	@Autowired
	private MainBoardDao mainBoardDao;
	@Autowired
	private CPUDao cpuDao;
	@Autowired
	private MemoryDao memoryDao;
	@Autowired
	private DiskDao diskDao;
	@Autowired
	private GraphicsDao graphicsDao;
	@Autowired
	private ChassisDao chassisDao;
	@Autowired
	private PowerDao powerDao;
	@Autowired
	private RadiatorDao radiatorDao;
	@Autowired
	private DriverDao driverDao;
	@Autowired
	private MonitorDao monitorDao;
	@Autowired
	private SoundBoxDao soundBoxDao;
	@Autowired
	private SoundCardDao soundCardDao;
	@Autowired
	private NetworkAdapterDao networkAdapterDao;
	@Autowired
	private ValueDao valueDao;

	public void setMainBoardDao(MainBoardDao mainBoardDao) {
		this.mainBoardDao = mainBoardDao;
	}

	public void setCpuDao(CPUDao cpuDao) {
		this.cpuDao = cpuDao;
	}

	public void setMemoryDao(MemoryDao memoryDao) {
		this.memoryDao = memoryDao;
	}

	public void setDiskDao(DiskDao diskDao) {
		this.diskDao = diskDao;
	}

	public void setGraphicsDao(GraphicsDao graphicsDao) {
		this.graphicsDao = graphicsDao;
	}

	public void setChassisDao(ChassisDao chassisDao) {
		this.chassisDao = chassisDao;
	}

	public void setPowerDao(PowerDao powerDao) {
		this.powerDao = powerDao;
	}

	public void setRadiatorDao(RadiatorDao radiatorDao) {
		this.radiatorDao = radiatorDao;
	}

	public void setDriverDao(DriverDao driverDao) {
		this.driverDao = driverDao;
	}

	public void setMonitorDao(MonitorDao monitorDao) {
		this.monitorDao = monitorDao;
	}

	public void setSoundBoxDao(SoundBoxDao soundBoxDao) {
		this.soundBoxDao = soundBoxDao;
	}

	public void setSoundCardDao(SoundCardDao soundCardDao) {
		this.soundCardDao = soundCardDao;
	}

	public void setNetworkAdapterDao(NetworkAdapterDao networkAdapterDao) {
		this.networkAdapterDao = networkAdapterDao;
	}

	public void setValueDao(ValueDao valueDao) {
		this.valueDao = valueDao;
	}

	@Override
	public HardwareSet fillHardwareSet(HardwareSet hardwareSet) {
		if (hardwareSet == null) {
			return null;
		}
		MainBoard mainBoard = hardwareSet.getMainBoard();
		if (mainBoard != null) {
			hardwareSet.setMainBoard(mainBoardDao.getOneById(mainBoard.getId()));
		}
		CPU cpu = hardwareSet.getCpu();
		if (cpu != null) {
			hardwareSet.setCpu(cpuDao.getOneById(cpu.getId()));
		}
		Memory memory = hardwareSet.getMemory();
		if (memory != null) {
			hardwareSet.setMemory(memoryDao.getOneById(memory.getId()));
		}
		Disk disk = hardwareSet.getDisk();
		if (disk != null) {
			hardwareSet.setDisk(diskDao.getOneById(disk.getId()));
		}
		Graphics graphics = hardwareSet.getGraphics();
		if (graphics != null) {
			hardwareSet.setGraphics(graphicsDao.getOneById(graphics.getId()));
		}
		Chassis chassis = hardwareSet.getChassis();
		if (chassis != null) {
			hardwareSet.setChassis(chassisDao.getOneById(chassis.getId()));
		}
		Power power = hardwareSet.getPower();
		if (power != null) {
			hardwareSet.setPower(powerDao.getOneById(power.getId()));
		}
		Radiator radiator = hardwareSet.getRadiator();
		if (radiator != null) {
			hardwareSet.setRadiator(radiatorDao.getOneById(radiator.getId()));
		}
		Driver driver = hardwareSet.getDriver();
		if (driver != null) {
			hardwareSet.setDriver(driverDao.getOneById(driver.getId()));
		}
		Monitor monitor = hardwareSet.getMonitor();
		if (monitor != null) {
			hardwareSet.setMonitor(monitorDao.getOneById(monitor.getId()));
		}
		SoundBox soundBox = hardwareSet.getSoundBox();
		if (soundBox != null) {
			hardwareSet.setSoundBox(soundBoxDao.getOneById(soundBox.getId()));
		}
		SoundCard soundCard = hardwareSet.getSoundCard();
		if (soundCard != null) {
			hardwareSet.setSoundCard(soundCardDao.getOneById(soundCard.getId()));
		}
		NetworkAdapter networkAdapter = hardwareSet.getNetworkAdapter();
		if (networkAdapter != null) {
			hardwareSet.setNetworkAdapter(networkAdapterDao.getOneById(networkAdapter.getId()));
		}
		return hardwareSet;
	}

	@Override
	public Scheme fillScheme(Scheme scheme) {
		if (scheme == null) {
			return null;
		}
		MainBoard mainBoard = scheme.getMainBoard();
		if (mainBoard != null) {
			scheme.setMainBoard(mainBoardDao.getOneById(mainBoard.getId()));
		}
		CPU cpu = scheme.getCpu();
		if (cpu != null) {
			scheme.setCpu(cpuDao.getOneById(cpu.getId()));
		}
		Memory memory = scheme.getMemory();
		if (memory != null) {
			scheme.setMemory(memoryDao.getOneById(memory.getId()));
		}
		Disk disk = scheme.getDisk();
		if (disk != null) {
			scheme.setDisk(diskDao.getOneById(disk.getId()));
		}
		Graphics graphics = scheme.getGraphics();
		if (graphics != null) {
			scheme.setGraphics(graphicsDao.getOneById(graphics.getId()));
		}
		Chassis chassis = scheme.getChassis();
		if (chassis != null) {
			scheme.setChassis(chassisDao.getOneById(chassis.getId()));
		}
		Power power = scheme.getPower();
		if (power != null) {
			scheme.setPower(powerDao.getOneById(power.getId()));
		}
		Radiator radiator = scheme.getRadiator();
		if (radiator != null) {
			scheme.setRadiator(radiatorDao.getOneById(radiator.getId()));
		}
		Driver driver = scheme.getDriver();
		if (driver != null) {
			scheme.setDriver(driverDao.getOneById(driver.getId()));
		}
		Monitor monitor = scheme.getMonitor();
		if (monitor != null) {
			scheme.setMonitor(monitorDao.getOneById(monitor.getId()));
		}
		SoundBox soundBox = scheme.getSoundBox();
		if (soundBox != null) {
			scheme.setSoundBox(soundBoxDao.getOneById(soundBox.getId()));
		}
		SoundCard soundCard = scheme.getSoundCard();
		if (soundCard != null) {
			scheme.setSoundCard(soundCardDao.getOneById(soundCard.getId()));
		}
		NetworkAdapter networkAdapter = scheme.getNetworkAdapter();
		if (networkAdapter != null) {
			scheme.setNetworkAdapter(networkAdapterDao.getOneById(networkAdapter.getId()));
		}
		scheme.setType(valueDao.getValue(scheme.getType().getKey()));
		scheme.setTime(new Date(System.currentTimeMillis()));
		int price = 0;
		if (scheme.getMainBoard() != null) {
			price += scheme.getMainBoard().getoPrice();
		}
		if (scheme.getCpu() != null) {
			price += scheme.getCpu().getoPrice();
		}
		if (scheme.getMemory() != null) {
			price += scheme.getMemory().getoPrice()*scheme.getMemoryNum();
		}
		if (scheme.getDisk() != null) {
			price += scheme.getDisk().getoPrice()*scheme.getDiskNum();
		}
		if (scheme.getGraphics() != null) {
			price += scheme.getGraphics().getoPrice();
		}
		if (scheme.getChassis() != null) {
			price += scheme.getChassis().getoPrice();
		}
		if (scheme.getPower() != null) {
			price += scheme.getPower().getoPrice();
		}
		if (scheme.getRadiator() != null) {
			price += scheme.getRadiator().getoPrice();
		}
		if (scheme.getDriver() != null) {
			price += scheme.getDriver().getoPrice();
		}
		if (scheme.getMonitor() != null) {
			price += scheme.getMonitor().getoPrice();
		}
		if (scheme.getSoundBox() != null) {
			price += scheme.getSoundBox().getoPrice();
		}
		if (scheme.getSoundCard() != null) {
			price += scheme.getSoundCard().getoPrice();
		}
		if (scheme.getNetworkAdapter() != null) {
			price += scheme.getNetworkAdapter().getoPrice();
		}
		scheme.setPrice(price);
		return scheme;
	}

}
